package com.ruoyi.appointment.service.impl;


import com.ruoyi.appointment.dao.CcsProcessDao;
import com.ruoyi.appointment.domain.CcsProcess;
import com.ruoyi.appointment.dto.ProcessDTO;
import com.ruoyi.appointment.enums.ProcessStatusEnum;
import com.ruoyi.appointment.enums.ProcessTypeEnum;
import com.ruoyi.appointment.service.CcsProcessService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

@Service
public class CcsProcessServiceImpl implements CcsProcessService {

    @Autowired
    private CcsProcessDao baseDao;

//    public CcsProcessServiceImpl(CcsProcessDao baseDao) {
//        this.baseDao = baseDao;
//    }


    @Override
    public AjaxResult savaProcess(ProcessDTO processDTO, Long userId) {
        // 封装信息
        CcsProcess ccsProcess = new CcsProcess();
        BeanUtils.copyBeanProp(ccsProcess, processDTO);

        ccsProcess.setUserId(userId);
        ccsProcess.setImg(processDTO.getImages().stream().collect(Collectors.joining(",")));
        ccsProcess.setAttachment(processDTO.getAttachmentList().stream().collect(Collectors.joining(",")));

        // 默认为未处理
        ccsProcess.setStatus(ProcessStatusEnum.INIT.getCode());
        return AjaxResult.success(baseDao.save(ccsProcess));
    }

    @Override
    public AjaxResult getProcess(Long userId) {
        return AjaxResult.success(baseDao.selectProcessList(userId));
    }

    @Override
    public AjaxResult passProcess(Long processId) {
        // 查询当前审核信息
        CcsProcess ccsProcess = baseDao.getById(processId);

        // 修改审核状态为通过
        ccsProcess.setStatus(ProcessStatusEnum.PASS.getCode());
        baseDao.updateById(ccsProcess);

        // 根据审核类型不同实现不同功能
        allowPower(ccsProcess);

        return AjaxResult.success();
    }

    /**
     * 实现不同审核类型的功能
     * @param ccsProcess 审核信息
     */
    private void allowPower(CcsProcess ccsProcess) {
        Integer type = ccsProcess.getType();

        // 中医
        if (ProcessTypeEnum.CN_MEDICINE.getCode().equals(type)) {
            baseDao.buildPowerForCNMedicine(ccsProcess, "doctor");
        }
        // 口腔
        else if (ProcessTypeEnum.ORAL_CAVITY.getCode().equals(type)) {
            baseDao.buildPowerForCNMedicine(ccsProcess, "oral_cavity");
        }
        // 公卫
        else if (ProcessTypeEnum.PUBLIC_HEALTH.getCode().equals(type)) {
            baseDao.buildPowerForCNMedicine(ccsProcess, "public_health");
        }
        // 其他
        else if (ProcessTypeEnum.OTHER.getCode().equals(type)) {
            // todo
//            baseDao.buildPowerForCNMedicine(ccsProcess, "other");
        }
    }

}
